package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.SupplierBean;
import com.project.service.ISupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("supplier")
@Api(tags = "供应商")
public class SupplierController {
    @Autowired
    private ISupplierService supplierService;
    @RequestMapping("findByItem")
    @ApiOperation(value = "分页查询供应商")
    public IPage<SupplierBean> findByItem(Integer pageNO, String supplierName, String scope){
        return supplierService.findByItem(pageNO, supplierName, scope);
    }
    @RequestMapping("findById")
    @ApiOperation(value = "根据供应商id查看供应商的详细信息")
    public SupplierBean findById(Integer supplierId){
        return supplierService.findById(supplierId);
    }
    @RequestMapping("update")
    @ApiOperation(value = "根据供应商id修改负责人和负责人电话")
    public String update(Integer supplierId,String headName,String headPhone){
        supplierService.update(supplierId, headName, headPhone);
        return "ok";
    }
    @RequestMapping("add")
    @ApiOperation(value = "添加供应商")
    public String add(SupplierBean supplier){
        if (supplierService.add(supplier)){
            return "ok";
        }else {
            return "no";
        }
    }
    @RequestMapping("showExcelData")
    public List<SupplierBean> showExcelData(MultipartFile excelFile) throws Exception {
        if (excelFile !=null && !excelFile.isEmpty()) {
            ImportParams params = new ImportParams();
            params.setSheetNum(1);
            params.setTitleRows(0);
            params.setHeadRows(1);

            List<SupplierBean> list = ExcelImportUtil.importExcel(excelFile.getInputStream(),
                    SupplierBean.class, params);
            return list;
        }

        return null;
    }

    @RequestMapping("addList")
    public String addList(MultipartFile excelFile) throws Exception {
        if (excelFile !=null && !excelFile.isEmpty()) {
            ImportParams params = new ImportParams();
            params.setSheetNum(1);
            params.setTitleRows(0);
            params.setHeadRows(1);

            List<SupplierBean> list = ExcelImportUtil.importExcel(excelFile.getInputStream(),
                    SupplierBean.class, params);
            supplierService.addList(list);
        }
        return "ok";
    }

    @RequestMapping("findAll")
    @ApiOperation(value = "查询所有供应商")
    public List<SupplierBean> findAll(){
        return supplierService.findAll();
    }
}
